
// javascript phaser game
var game;

window.onload = function() {
    var gameConfig = {
        type: Phaser.CANVAS,
        width: 800,
        height: 600,
        scene: [playGame],
        physics: {
            default: 'arcade',
            arcade: {
                gravity: { y: 200 },
                debug: false
            }
        },
    };
    game = new Phaser.Game(gameConfig);
    // window.addEventListener("resize", resize, false);
}
class playGame extends Phaser.Scene {
    constructor() {
        super("playGame");
    }
    preload() {
        this.load.image('sky', 'assets/sky.png');
        this.load.image('ground', 'assets/platform.png');
        this.load.image('star', 'assets/star.png');
        this.load.image('bomb', 'assets/diamond.png');
        this.load.spritesheet({
            key: 'dude',
            url: 'assets/dude.png',
            frameConfig: {
                frameWidth: 32,
                frameHeight: 48,
                stratFrame: 128,
                endFrame: 160,
            }
        });
    }
    create() {
        this.add.image(400, 300, 'sky');
        this.platforms = this.physics.add.staticGroup();
        this.platforms.create(400, 568, 'ground').setScale(2).refreshBody();
        this.platforms.create(600, 400, 'ground');
        this.platforms.create(50, 250, 'ground');
        this.platforms.create(750, 220, 'ground');

        // 
        this.player = this.physics.add.sprite(100, 450, 'dude');
        this.player.setBounce(0.2);
        this.player.setCollideWorldBounds(true);
        this.player.body.setGravityY(300); // 精灵体设置重力

        this.physics.add.collider(this.player, this.platforms);  // 碰撞

        // 动画
        this.anims.create({
            key: 'left',
            frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
            frameRate: 10,
            repeat: -1
        });

        this.anims.create({
            key: 'turn',
            frames: [ { key: 'dude', frame: 4 } ],
            frameRate: 20
        });
         
        this.anims.create({
            key: 'right',
            frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
            frameRate: 10,
            repeat: -1
        });
    
        // 事件监听
        this.cursors = this.input.keyboard.createCursorKeys();
        

        // 星星
        this.stars = this.physics.add.group({
            key: 'star',
            repeat: 11,
            setXY: { x: 12, y: 0, stepX: 70 }
        });
        this.stars.children.iterate(child => {
            child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
        });
        this.physics.add.collider(this.stars, this.platforms);

        // 收集星星 todo计分
        this.physics.add.overlap(this.player, this.stars, this.collectStar, null, this);

        // todo 障碍物
        this.bombs = this.physics.add.group();
        // this.physics.add()
    }

    addBomb(){
        // 增加随机炸弹。
        this.bombs.create(); 
    }

    collectStar(player, star) {
        star.disableBody(true, true);
        
        // todo 星星收集完了就重新来
        
    }

    playGame(){
        // 游戏操作
        
    }

    update(time, delta) {
        this.playGame(); // 游戏状态更新
        
    }
}

/**
 * 碰撞得分
 * 制造障碍物
 * 游戏结束
 */


// 坐标定位函数
/**
 * 一单位的长度
 * 
 **/
